﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Controls;
using ViCode_LeVi.Utilities;
using FES.Intimex.Client.Shared.Splash;
using ViCode_LeVi.KeyGen;

namespace ViCode_LeVi
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class WindowEditInfo : Window
    {
        public WindowEditInfo()
        {
            InitializeComponent();
            ProjectData.Action_FileSave_Changed = (s) =>
                {
                    this.Title = s;
                };
            this.Loaded += new RoutedEventHandler(Window1_Loaded);
            this.Closing += new System.ComponentModel.CancelEventHandler(Window1_Closing);
        }

        void Window1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (System.IO.File.Exists(ProjectData.FileSave))
            {
                string s = "Do you want to save this Project?";
                var check = MessageBox.Show(s, "Warning", MessageBoxButton.YesNoCancel);
                if(check== MessageBoxResult.Yes)                
                {
                    ProjectData.Intance.Save();
                    return;
                }
                if(check == MessageBoxResult.Cancel)
                {
                     e.Cancel = true;
                }
            }
        }
        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            Splasher.CloseSplash();
            this.Hide();
            KeyGenHelper.CheckKey();
            this.Show();

            //MenuItem_Lang.DataContext = Langs.LangManager.Intance;
            //Set last
            var data = ProjectData.CreateDefaultRootData();
            ProjectData.Intance = data;
            SetDataContext();
            //DockPanel_Content.DataContextChanged += new DependencyPropertyChangedEventHandler(DockPanel_Content_DataContextChanged);
            //DockPanel_Content.DataContext = TabItem_Step1.DataContext = data.Steps[0];
            SetupHotkey();
        }

        private void SetupHotkey()
        {
            this.PreviewKeyUp += new KeyEventHandler(Window1_PreviewKeyUp);
        }

        void Window1_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            if (System.Windows.Input.Keyboard.Modifiers == System.Windows.Input.ModifierKeys.Control)
            {
               
                
                 if (e.Key == Key.S)
                    MenuItem_Save_Click(null, null);
                
               
                
            }
            else
                if (e.Key == Key.F1)
                {
                    MenuItem_HelpIndex_Click(null, null);
                }
        }

        void DockPanel_Content_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            //Panel tabItem = sender as Panel;
            //if(tabItem != null)
            //{
            //    var stepInfo = tabItem.DataContext as StepInfo;
            //    if(stepInfo != null)
            //    {
            //        VMTabControl_VMQuestionAnswer tab = new VMTabControl_VMQuestionAnswer();
            //        tabItem.Children.Add(tab);
            //        tab.SetupDataContext(stepInfo.QuestionAnwserGroups);                     
            //    }
            //}     
        }

       
        private void MenuItem_Save_Click(object sender, RoutedEventArgs e)
        {
            //LostFocus4UpdateBinding();
            ////RootData root = TabControl_Root.DataContext as RootData;
            ////root.Save();
            //ProjectData.Intance.Save();
            this.Close();
        }

        private void MenuItem_SaveAs_Click(object sender, RoutedEventArgs e)
        {
            LostFocus4UpdateBinding();
            //RootData root = TabControl_Root.DataContext as RootData;
            //root.SaveAs();
            ProjectData.Intance.SaveAs();
        }

       

        private void SetDataContext()
        {
           

            //TabControl_Root.DataContext = null;
            //TabControl_Root.DataContext = RootData.Intance;

            //if (RootData.Intance != null)
            //{
            //    RootData.Intance.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(Intance_PropertyChanged);
            //    RootData.Intance.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Intance_PropertyChanged);
            //}
            //TabControl_Root.SelectionChanged -= new SelectionChangedEventHandler(TabControl_Root_SelectionChanged);
            //TabControl_Root.SelectionChanged += new SelectionChangedEventHandler(TabControl_Root_SelectionChanged);
            ListView_Enum_Impact_Items.ItemsSource = ProjectData.Intance.Static_E_Risk_Impact_Intance.Intances;
            ListView_Enum_Probability_Items.ItemsSource = ProjectData.Intance.Static_E_Risk_Probability_Intance.Intances;
            
        }

        void TabControl_Root_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (RootData.Intance != null)
            {
                RootData.Intance.Step_Current = TabControl_Root.SelectedItem == null ? null : ((TabItem)TabControl_Root.SelectedItem).DataContext as StepInfo;
            }
        }

        void Intance_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //if (e.PropertyName == "Step_Current")
            //{
            //    var step = TabControl_Root.Items.Cast<TabItem>().FirstOrDefault(c => c.DataContext == RootData.Intance.Step_Current);
            //    if (step != null)
            //        TabControl_Root.SelectedItem = step;
            //}
        }
        private void LostFocus4UpdateBinding()
        {
            var tabItem = TabControl_Root.SelectedItem as TabItem;
            TabItem_Step1.Focus();
            if (tabItem != null)
                tabItem.Focus();
        }
        public void RefreshUI()
        {
            TabControl_Root.UpdateLayout();
            TabItem_Step1.UpdateLayout();
            TabItem_Step2.UpdateLayout();
            
            VMTabHeaders.Refresh();
            VMTextBlockExt_ControlTextInfoInDatas.RefreshUI();
            CommandManager.InvalidateRequerySuggested();


        }
        private void ShowProjectInfo()
        {
            VMProjectInfo project = new VMProjectInfo() { DataContext = ProjectData.Intance };
            Window w = new Window()
            {
                Icon = Application.Current.MainWindow.Icon,
                Content = project
                    ,
                SizeToContent = SizeToContent.Height
                ,
                Title = "Project Info"
                ,
                WindowState = WindowState.Normal
                    ,
                WindowStartupLocation = WindowStartupLocation.CenterScreen
                    //,Height = System.Windows.SystemParameters.PrimaryScreenHeight * 8 / 9
                ,
                Width = System.Windows.SystemParameters.PrimaryScreenWidth * 4 / 9
            };
            //w.Width =
            w.ShowDialog();
        }
        

       

        private void MenuItem_Quit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MenuItem_HelpIndex_Click(object sender, RoutedEventArgs e)
        {
            string sPath = FileFolder.AppInfo.AppDirectory + "/help.chm";
            FileFolder.RunFileHelper.RunFile(sPath);
        }

        

       
        private void MenuItem_CloseProject_Click(object sender, RoutedEventArgs e)
        {
            MenuItem_Save_Click(null, null);
            TabControl_Root.SelectedIndex = 0;
            
        }
    }
}
